//
//  XOCategoryViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/3/19.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "XOCategoryViewController.h"

#import "EJSCategoryTableViewCell.h"
#import "EJSCategoryCollectionViewCell.h"
#import "EJSCategoryCollectionReusableView.h"
#import "EJSHomeBannerView.h"
#import "EJSGoodsListViewController.h"
#import <UIView+BlocksKit.h>

#import "EJSGoodsCategoryRequest.h"
#import "EJSAdRequest.h"
#import "XOPlatformCategoryListRequest.h"

@interface XOCategoryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) EJSHomeBannerView *bannerView;
@property (strong, nonatomic) NSMutableArray *firstCategoryList;

@property (strong, nonatomic) NSMutableArray *categoryAdList;
//@property (strong, nonatomic) EJSCategoryModel *firstCategoryModel;
@property (strong, nonatomic) XOPlatformCategoryModel *firstCategoryModel;

@end

@implementation XOCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getFirstCategory];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configOwnViews
{
    [self configTableView];
    [self configCollectionView];
}
- (void)configTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSCategoryTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSCategoryTableViewCell"];
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.tableFooterView = [UIView new];
}
- (void)configCollectionView
{
    [self.collectionView registerNib:[UINib nibWithNibName:@"EJSCategoryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"EJSCategoryCollectionViewCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"EJSCategoryCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"EJSCategoryCollectionReusableView"];
    
    
    CGFloat bannerViewWidth = ScreenWidth - self.tableView.width - 30;
    CGFloat bannerViewHeight = bannerViewWidth*130/244;
    
    self.bannerView = [[EJSHomeBannerView alloc] initWithFrame:CGRectMake(15, -bannerViewHeight, bannerViewWidth, bannerViewHeight)];
    [self.collectionView addSubview:self.bannerView];
}

- (void)setCategoryAdList:(NSMutableArray *)categoryAdList{
        _categoryAdList = categoryAdList;
        
        if(_categoryAdList.count > 0){
            self.collectionView.contentInset = UIEdgeInsetsMake(self.bannerView.height + 15, 0, 0, 0);
        }else{
            self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        }
        NSMutableArray *adImageUrls = [NSMutableArray array];
        for (EJSAdModel *model in _categoryAdList) {
            [adImageUrls addObject:model.img?model.img:@""];
        }
        [self.bannerView.cycleScrollView setImageURLStringsGroup:adImageUrls];
}


//- (void)setFirstCategoryModel:(EJSCategoryModel *)firstCategoryModel{
//    if(_firstCategoryModel != firstCategoryModel){
//        _firstCategoryModel = firstCategoryModel;
//        self.categoryAdList = [_firstCategoryModel.adList mutableCopy];
//        if(!_firstCategoryModel.adList){
//            FlyWeakSelf
//            EJSAdRequest *req = [EJSAdRequest new];
//            req.type = @"category";
//            req.categoryId = _firstCategoryModel.categoryId;
//            [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
//                weakSelf.categoryAdList = response.data;
//                _firstCategoryModel.adList = response.data;
//            } failHandler:^(BaseResponse *response) {
//                self.categoryAdList = nil;
//            }];
//        }
//    }
//}

- (void)setFirstCategoryModel:(XOPlatformCategoryModel *)firstCategoryModel {
    _firstCategoryModel = firstCategoryModel;
    self.categoryAdList = [_firstCategoryModel.adList mutableCopy];
    if(!_firstCategoryModel.adList){
        FlyWeakSelf
        EJSAdRequest *req = [EJSAdRequest new];
        req.type = @"category";
        req.sortId = _firstCategoryModel.sortId;
        [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
            weakSelf.categoryAdList = response.data;
            _firstCategoryModel.adList = response.data;
        } failHandler:^(BaseResponse *response) {
            self.categoryAdList = nil;
        }];
    }
}

- (void)getFirstCategory{
    FlyWeakSelf
    //获取一级分类
    XOPlatformCategoryListRequest *req = [XOPlatformCategoryListRequest new];
//    EJSGoodsCategoryRequest *req = [EJSGoodsCategoryRequest new];
    req.type = @"children";
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.firstCategoryList = response.data;
        [weakSelf.tableView reloadData];
        if(weakSelf.firstCategoryList.count > 0){
            [weakSelf.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
            self.firstCategoryModel =  weakSelf.firstCategoryList.firstObject;
        }
        [weakSelf.collectionView reloadData];
    } failHandler:^(BaseResponse *response) {
        
    }];
}

#pragma mark - CollectionView DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.firstCategoryModel.sons.count;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    EJSCategoryModel *model = self.firstCategoryModel.sons[section];
    return model.sons.count;
}

- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    EJSCategoryCollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSCategoryCollectionViewCell" forIndexPath:indexPath];
    XOPlatformCategoryModel *model = self.firstCategoryModel.sons[indexPath.section];
    XOPlatformCategoryModel *sonModel = model.sons[indexPath.item];
    [ccell setPlatformCategoryModel:sonModel];
    return ccell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(50, 80);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    EJSCategoryCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"EJSCategoryCollectionReusableView" forIndexPath:indexPath];
    XOPlatformCategoryModel *model = self.firstCategoryModel.sons[indexPath.section];
    headerView.categoryNameLabel.text = model.sortName;
    headerView.platformCategoryModel = model;
    [headerView bk_whenTapped:^{
        EJSGoodsListViewController *goodsListVC = [[EJSGoodsListViewController alloc] initWithNibName:@"EJSGoodsListViewController" bundle:nil];
        goodsListVC.platformCategoryModel = headerView.platformCategoryModel;
        [self.navigationController pushViewController:goodsListVC animated:YES];
    }];
    return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(ScreenWidth, 64);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 21;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    //item 宽度固定  每行显示三个  间距变化
    CGFloat spacing = (collectionView.width - 50*3 - 15*2)/2;
    return spacing;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XOPlatformCategoryModel *model = self.firstCategoryModel.sons[indexPath.section];
    XOPlatformCategoryModel *sonModel = model.sons[indexPath.item];
    EJSGoodsListViewController *goodsListVC = [[EJSGoodsListViewController alloc] initWithNibName:@"EJSGoodsListViewController" bundle:nil];
    goodsListVC.platformCategoryModel = sonModel;
    [self.navigationController pushViewController:goodsListVC animated:YES];
}

#pragma mark - TableView DataSource

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.firstCategoryList.count;
}

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    EJSCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSCategoryTableViewCell" forIndexPath:indexPath];
    
    XOPlatformCategoryModel *model = self.firstCategoryList[indexPath.row];
    cell.categoryNameLabel.text = model.sortName;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 62;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
     self.firstCategoryModel = self.firstCategoryList[indexPath.row];
    [self.collectionView reloadData];
    self.collectionView.contentOffset = CGPointMake(0, -self.collectionView.contentInset.top);
}

@end
